import { createRouter, createWebHashHistory } from 'vue-router'
import LoginPage from '../components/LoginPage.vue'
import HomePage from '../components/HomePage.vue'
import OperationalComponent from '../components/OperationalComponent.vue'
import OperationalAddComponent from '../components/OperationalAddComponent.vue'
import CategoryComponent from '../components/CategoryComponent.vue'
import GoodsManagerComponent from '../components/GoodsManagerComponent.vue'
import GoodsEditComponent from '../components/GoodsEditComponent.vue'
import OrderComponent from '../components/OrderComponent.vue'
import EvaluationComponent from '../components/EvaluationComponent.vue'
import StatisticalComponent from '../components/StatisticalComponent.vue'
import Account from './AccountStore'
// 定义页面名称枚举
export enum PageName {
    login = 'login',
    home = 'home'
}
// 定义路由
const shopRouter = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/login',
            name: PageName.login,
            component: LoginPage
        },
        {
            path: '/home',
            name: PageName.home,
            component: HomePage,
            children:[
                {
                    path:'operational',
                    component:OperationalComponent,
                    name:"Operational"
                },
                {
                    path:'operationalAdd',
                    component:OperationalAddComponent,
                    name:"OperationalAdd"
                },
                {
                    path:'category',
                    component:CategoryComponent,
                    name:"category"
                },
                {
                    path:'goodsEdit/:mode?/:id?',
                    component:GoodsEditComponent,
                    name:"goodsEdit",
                    props: true
                },
                {
                    path:'goodsManager',
                    component:GoodsManagerComponent,
                    name:"goodsManager"
                },
                {
                    path:'orderManager',
                    component:OrderComponent,
                    name:"orderManager"
                },
                {
                    path:'evaluationManager',
                    component:EvaluationComponent,
                    name:"evaluationManager"
                },
                {
                    path:'statisticalManager',
                    component:StatisticalComponent,
                    name:"statisticalManager"
                }
            ],
            redirect:'/home/operational'
        }
    ]
});
// 定义前置守卫
shopRouter.beforeEach((to)=>{
    // 如果用户未登录，且访问了非登录页，则重定向到登录页
    if ((to.name != PageName.login) && (!Account().isLogin || Account().info?.role != 'admin')) {
        return {
            name: PageName.login
        }
    }
})
// 导出路由
export default shopRouter;